package com.nhutcm.sms.Utils;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.nhutcm.sms.Utils.LogUtils.Level;
import com.nhutcm.sms.model.SMSClass;

public class JsonParserUtils {

	public static String TAG = "JsonParserUtils";

	public static List<SMSClass> parseStringToObject(String result) {
		List<SMSClass> list = new ArrayList<SMSClass>();
		result = "{response:" + result + "}";
		try {
			JSONObject jsonObj = new JSONObject(result);
			JSONArray jsonArr = jsonObj.getJSONArray("response");
			int len = jsonArr.length();
			for (int i = 0; i < len; i++) {
				JSONObject subJson = jsonArr.getJSONObject(i);

				String message = subJson.getString("message");
				message = message.replaceAll("<br>", "\n");

				String phone = subJson.getString("phone");
				String smsno = subJson.getString("smsno");

				SMSClass sms = new SMSClass(phone, smsno, message);
				list.add(sms);
			}
		} catch (JSONException e) {
			LogUtils.write(Level.ERROR, e.getMessage());
		}
		return list;
	}
}
